package shopping;

import java.util.Scanner;

/**
 * @author 29284
 * @since 2024/11/28 21:11
 */

class Main {
    public static void main(String[] args) {
        //提前存一些商品
        Product apple = new Product("苹果", 5.0, "水果", 10);
        Product banana = new Product("香蕉", 3.0, "水果", 8);
        Product coke = new Product("可口可乐", 5.0, "饮品", 10);
        Product milk = new Product("牛奶", 6.0, "饮品", 1);
        Product sprite = new Product("雪碧", 5.0, "饮品", 10);
        //定义购物车,定义容量
        ShoppingCart shoppingCart = new ShoppingCart(5);
        //用户添加商品
        Scanner sc = new Scanner(System.in);
        boolean run = true;
        System.out.println("请选择：");
        System.out.println("输入1，添加商品到购物车。");
        System.out.println("输入2，查看购物车中商品。");
        System.out.println("输入3，删除购物车中某商品。");
        System.out.println("输入4，结算购物车。");
        System.out.println("输入5，结束交易。");
        while (run) {
            int choice = sc.nextInt();
            switch (choice) {
                //添加商品到购物车
                case 1:
                    System.out.println("选择要添加的商品：apple,banana,coke,milk,sprite");
                    String goodChoice = sc.next();
                    System.out.println("需要购买的数量：");
                    int number = sc.nextInt();
                    if (goodChoice.equals("apple")) {
                        if (number > apple.getProductnum()){
                            System.out.println("商品数量不足，剩余商品数量为"+apple.getProductnum());
                        }else if (number <=apple.getProductnum()){
                            shoppingCart.addProduct(apple, number);
                        }
                    } else if (goodChoice.equals("banana")) {
                        if (number > banana.getProductnum()){
                            System.out.println("商品数量不足，剩余商品数量为"+banana.getProductnum());
                        }else if (number <=banana.getProductnum()){
                            shoppingCart.addProduct(banana, number);
                        }
                    } else if (goodChoice.equals("coke")) {
                        if (number > coke.getProductnum()){
                            System.out.println("商品数量不足，剩余商品数量为"+coke.getProductnum());
                        }else if (number <=coke.getProductnum()){
                            shoppingCart.addProduct(coke, number);
                        }
                    } else if (goodChoice.equals("milk")) {
                        if (number > milk.getProductnum()){
                            System.out.println("商品数量不足，剩余商品数量为"+milk.getProductnum());
                        }else if (number <=milk.getProductnum()) {
                            shoppingCart.addProduct(milk, number);
                        }
                    } else if (goodChoice.equals("sprite")) {
                        if (number > sprite.getProductnum()){
                            System.out.println("商品数量不足，剩余商品数量为"+sprite.getProductnum());
                        }else if (number <=sprite.getProductnum()){
                            shoppingCart.addProduct(sprite, number);
                        }
                    }
                    break;
                //查看现有商品
                case 2:
                    shoppingCart.displayProducts();
                    break;
                //删除商品
                case 3:
                    System.out.println("选择要删除的商品：apple,banana,coke,milk,sprite");
                    String deleteChoice = sc.next();
                    if (deleteChoice.equals("apple")) {
                        shoppingCart.removeProduct(apple);
                    }else if (deleteChoice.equals("banana")) {
                        shoppingCart.removeProduct(banana);
                    }else if (deleteChoice.equals("coke")) {
                        shoppingCart.removeProduct(coke);
                    }else if (deleteChoice.equals("milk")) {
                        shoppingCart.removeProduct(milk);
                    }else if (deleteChoice.equals("sprite")) {
                        shoppingCart.removeProduct(sprite);
                    }
                    break;
                //结算
                case 4:
                    double totalPrice = shoppingCart.getTotalPrice();
                    System.out.println("总价：" + totalPrice);
                    break;
                //结束交易
                case 5:
                    run = false;
                    break;
                default:
                    System.out.println("选择无效，请重新选择！");
            }
        }
        sc.close();
    }
}
